package logobj.dataAnalysis;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import logobj.config.ConfProperties;
import logobj.entity.bak.Dbdw;
import logobj.entity.bak.Shzip;
import logobj.utils.TimeUtils;

import java.io.*;
import java.util.*;

public class AnalysisLog {
    private Map<String , Object> jsonmap = new HashMap<>();
    private List<Dbdw> dbdwArrayList = new ArrayList<>();
    private List<Shzip> shzipArrayList = new ArrayList<>();
    {
        jsonmap.put("dbdwArrayList",dbdwArrayList);
        jsonmap.put("shzipArrayList",shzipArrayList);
    }


    /**
     * 读取文件
     * */
    public Map<String , Object> loadLog(String filename){
        List<String> loglineList=new ArrayList<>();
        int reint = 0;
        FileInputStream inputStream = null;
        Scanner sc = null;
        String line = null;
        try{
            inputStream = new FileInputStream(filename);
            sc = new Scanner(inputStream, "UTF-8");
            while(sc.hasNextLine()) {
                 line = sc.nextLine();
                //去掉日志前面的多余字符，保留json字符串
                if(line.indexOf("{") > -1){
                    loglineList.add(line.substring(line.indexOf("{")));
                }else {
                    loglineList.add(line);
                }
                reint ++;
            }
            if(sc.ioException() != null) {
                throw sc.ioException();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            if(inputStream != null) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(sc != null) {
                sc.close();
            }

        }
        System.gc();
        //loglineList.remove(reint-1);
        reint = loglineList.size();
        for(int i =0;i< reint;i++){
            //如果是快速模式，则需要进行去掉残缺的行
            if("fast".equals(ConfProperties.file_incision_model)  ){
                if(i != 0 && i+1 != reint ){
                    analysisStrToJson(loglineList.get(i));
                }
            }else {
                analysisStrToJson(loglineList.get(i));
            }

        }
        return jsonmap;
    }


    /**
     * json字符串解析
     * @param jsonstr json格式的字符串
     * @return void
     * */
    public void analysisStrToJson(String jsonstr){

        JSONObject jsonobj = JSON.parseObject(jsonstr); //将json字符串转换成jsonObject对象
        Dbdw dbdw = new Dbdw();
        Shzip shzip= new Shzip();

        //字符串转换json对象
        JSONObject jsonInputData = jsonobj.getJSONObject("InputData");
        JSONObject jsonAssets = jsonInputData.getJSONObject("assets");
        JSONObject jsonCheckResult = jsonobj.getJSONObject("CheckResult");
        JSONObject jsonIoc = jsonCheckResult.getJSONObject("ioc");
        JSONObject mc_geo_data = jsonInputData.getJSONObject("mc_geo_data");

        //获取json数据

        if(jsonIoc != null){
            shzip.setMachine(jsonInputData.getString("machine"));
            shzip.setStatus(jsonIoc.getString("status"));
            shzip.setIntel_uuid(jsonIoc.getString("intel_uuid"));
            shzip.setIoc_type(jsonIoc.getString("ioc_type"));
            shzip.setIntel_type(jsonIoc.getString("intel_type"));
            shzip.setSeverity(jsonIoc.getString("severity"));
            shzip.setConfidence(jsonIoc.getString("confidence"));
            shzip.setFamily(jsonIoc.getString("family"));
            shzip.setSource(jsonIoc.getString("source"));
            shzip.setMain_tag(jsonIoc.getString("main_tag"));
            shzip.setIs_apt(jsonIoc.getString("is_apt"));
            shzip.setSource_type(jsonIoc.getString("source_type"));
            shzip.setBasic_tag(jsonIoc.getString("basic_tag"));
            shzip.setRksj(TimeUtils.getStringNowDate());
            if(mc_geo_data != null){
                shzip.setMc_geo_latitude(mc_geo_data.getString("Latitude"));
                shzip.setMc_geo_longitude(mc_geo_data.getString("Longitude"));
            }
            shzipArrayList.add(shzip);
        }

        if(jsonAssets != null){
            dbdw.setMachine(jsonInputData.getString("machine"));

            dbdw.setAssets_id(jsonAssets.getString("id"));
            dbdw.setAssets_status(jsonAssets.getString("status"));
            dbdw.setAssets_name(jsonAssets.getString("name"));
            dbdw.setAssets_ip(jsonAssets.getString("ip"));
            dbdw.setAssets_section(jsonAssets.getString("section"));
            dbdw.setAssets_location(jsonAssets.getString("location"));
            dbdw.setAssets_level(jsonAssets.getString("level"));
            dbdw.setAssets_ext(jsonAssets.getString("ext"));
            dbdw.setAssets_mac(jsonAssets.getString("mac"));

            dbdw.setStatus(jsonIoc.getString("status"));
            dbdw.setIntel_uuid(jsonIoc.getString("intel_uuid"));
            dbdw.setIoc_type(jsonIoc.getString("ioc_type"));
            dbdw.setIntel_type(jsonIoc.getString("intel_type"));
            dbdw.setSeverity(jsonIoc.getString("severity"));
            dbdw.setConfidence(jsonIoc.getString("confidence"));
            dbdw.setFamily(jsonIoc.getString("family"));
            dbdw.setSource(jsonIoc.getString("source"));
            dbdw.setMain_tag(jsonIoc.getString("main_tag"));
            dbdw.setIs_apt(jsonIoc.getString("is_apt"));
            dbdw.setSource_type(jsonIoc.getString("source_type"));
            dbdw.setBasic_tag(jsonIoc.getString("basic_tag"));
            dbdw.setRksj(TimeUtils.getStringNowDate());
            dbdwArrayList.add(dbdw);
        }

    }
}
